package com.dfrz.dao;

import com.dfrz.entry.Order;
import com.dfrz.util.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class OrderDao {
    public int insertOrder(Order order){
        int count=0;
        String sqlStr="insert into order(orderNo,userid,totalprice,status,num,goodsid,remarks,businessid,deliveryid)values" +
                "(?,?,?,?,?,?,?,?,?)";
        QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource());
        try {
            count=queryRunner.update(sqlStr,order.getOrderNo(),
                    order.getUserid(),
                    order.getTotalprice(),
                    order.getStatus(),
                    order.getNum(),
                    order.getGoodsid(),
                    order.getRemarks(),
                    order.getBusinessid(),
                    order.getDeliveryid());
        } catch (SQLException e) {
            e.printStackTrace();

        }
        return count;
    }
    public int deleteOrder(Integer id){
        int count = 0;
        String sqlStr="update order set status=-1 where id=?";
        QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource());
        try {
            count=queryRunner.update(sqlStr,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;

    }
    public int updateOrder(Order order){
        int count = 0;
        String sqlStr="update order set userid=?,totalprice=?,num=?,goodsid=?,remarks=?,businessid=?,deliveryid=?";
        QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource());
        try {
            count=queryRunner.update(sqlStr,order.getUserid(),order.getTotalprice(),order.getNum(),order.getGoodsid(),order.getRemarks(),order.getBusinessid(),order.getDeliveryid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
    public List<Order> queryOrderByPage(Order order,int limit,int startNum){
        List<Order>listOrder=null;
        String sqlStr="select * from order where 1=1";
        if(order.getOrderNo()!=null||!"".equals(order.getOrderNo())){
            sqlStr+=" and orderNo="+"'"+order.getOrderNo()+"'";
        }
        if (order.getUserid()!=null||!"".equals(order.getUserid())){
            sqlStr+=" and userid="+order.getUserid();
        }
        QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource());
        try {
            listOrder=queryRunner.query(sqlStr,new BeanListHandler<Order>(Order.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return listOrder;
    }
}
